package com.example.music.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.music.R;
import com.example.music.entity.DataMusic;

import java.util.List;

public class MusicsAdapter extends RecyclerView.Adapter<MusicsAdapter.ViewHolder> {

    private List<DataMusic> textData;
    private Context context;
    private Event event;

    public MusicsAdapter(List<DataMusic> textData, Context context,Event event) {
        this.textData = textData;
        this.context = context;
        this.event = event;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_music, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        DataMusic dataMusic = textData.get(i);
        viewHolder.textView.setText(dataMusic.getAuthor()+"·"+dataMusic.getName());
        Glide.with(context).load(dataMusic.getImg_url())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                .into(viewHolder.imageView);
        viewHolder.itemView.setOnClickListener(v -> event.click(dataMusic));
    }

    @Override
    public int getItemCount() {
        return textData==null?0:textData.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_item);
            imageView = itemView.findViewById(R.id.img_item);
        }
    }

    public interface Event{
        void click(DataMusic dataMusic);
    }
}

